package com.chaoyue.learning.service.impl;


import com.chaoyue.base.model.RestResponse;
import com.chaoyue.content.beans.po.CoursePublish;
import com.chaoyue.learning.beans.dto.CyCourseTablesDto;
import com.chaoyue.learning.feignclient.ContentServiceClient;
import com.chaoyue.learning.feignclient.MediaServiceClient;
import com.chaoyue.learning.service.LearningService;
import com.chaoyue.learning.service.MyCourseTablesService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@SuppressWarnings({"all"})
@Service
@Slf4j
//学习资格状态 [{"code":"702001","desc":"正常学习"},{"code":"702002","desc":"没有选课或选课后没有支付"},{"code":"702003","desc":"已过期需要申请续期或重新支付"}]
public class LearningServiceImpl implements LearningService {
    @Autowired
    ContentServiceClient contentServiceClient;
    @Autowired
    MyCourseTablesService courseTablesService;
    @Autowired
    MediaServiceClient mediaServiceClient;

    @Override
    public RestResponse<String> getVideo(String userId, Long courseId, Long teachplanId, String mediaId) {

        CoursePublish coursepublish = contentServiceClient.getCoursepublish(courseId);
        if (coursepublish == null) {
            return RestResponse.validfail("课程信息不存在");
        }
        //判断用户是否登录
        if (!StringUtils.isEmpty(userId)) {
            //获取学习资格
            CyCourseTablesDto learningStatus = courseTablesService.getLearningStatus(userId, courseId);
            String learnStatus = learningStatus.getLearnStatus();
            if (learnStatus.equals("702002")) {
                return RestResponse.validfail("课程没有选课或选课后没有支付");
            } else if (learnStatus.equals("702003")) {
                return RestResponse.validfail("课程已过期需要申请续期或重新支付");
            } else {
                return mediaServiceClient.getPlayUrlByMediaId(mediaId);
            }
        }
        String charge = coursepublish.getCharge();
        if(charge.equals("201000")){
            return mediaServiceClient.getPlayUrlByMediaId(mediaId);
        }
        return RestResponse.validfail("请购买课程后继续学习");
    }
}
